<?php

namespace App\Model\User;

use App\Constants\IdentityConstants;
use App\Model\Model;

class UserModel extends Model
{
    protected ?string $connection = 'organ';

    protected ?string $table = 'organ_user';

    protected array $guarded = [];

    protected array $casts = [
        'id' => 'int',
        'organ_id'  => 'int',
        'user_name' => 'string',
        'user_account' => 'string',
        'user_password' => 'string',
        'user_phone' => 'string',
        'user_email' => 'string',
        'status' => 'int',
        'pu_key' => 'string',
        'pv_key' => 'string',
        'created_at'  => 'date:Y-m-d h:i:s',
        'updated_at'  => 'date:Y-m-d h:i:s',
    ];

    public function scopeWithCustomCondition($query)
    {
        return $query->where('organ_id', getOrganIdByOrganCode());
    }

    public function getUserTypeAttribute($value)
    {
        return  match ((int)$value) {
            IdentityConstants::SP_USER->value => IdentityConstants::SP_USER->getMessage(),
            IdentityConstants::SSR_USER->value => IdentityConstants::SSR_USER->getMessage(),
            default => IdentityConstants::SR_USER->getMessage(),
        };
    }

}